package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cc
 * @version 1.0
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    private DtsCategoryService dtsCategoryService;

    /**
     *  商品类目列表
     * 接口地址 : http://localhost:8083/admin/category/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissions("admin:category:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort", defaultValue = "add_time") String sort,
                       @RequestParam(value = "order", defaultValue = "desc") String order){

        IPage<DtsCategory> dtsCategoryIPage = dtsCategoryService.findAll(page, limit, sort, order);
        Map<String, Object> map = new HashMap<>();
        map.put("total", dtsCategoryIPage.getTotal());
        map.put("items", dtsCategoryIPage.getRecords());
        return ResponseUtil.ok(map);
    }

    /**
     *    商品一级类目列表
     * 接口地址 : http://localhost:8083/admin/category/l1
     */
    @RequiresPermissions("admin:category:l1")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "商品一级类目列表")
    @GetMapping("/l1")
    public Object l1(){
       List<CatVo>  l1 = dtsCategoryService.l1();
        return ResponseUtil.ok(l1);
    }


    /**
     * 接口地址 : http://localhost:8083/admin/category/create
     *    url: '/category/create',
     *     method: 'post',
     *     data
     */
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "添加")
    @PostMapping("/create")
    public  Object create(@RequestBody DtsCategory dtsCategory){
            dtsCategoryService.create(dtsCategory);
        return ResponseUtil.ok();
    }

    /**
     *     url: '/category/update',
     *     method: 'post',
     *     data
     */
    @RequiresPermissions("admin:category:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory dtsCategory){
            dtsCategoryService.update(dtsCategory);
        return ResponseUtil.ok();
    }

    /**
     *     url: '/category/delete',
     *     method: 'post',
     *     data
     */
    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        Integer id = dtsCategory.getId();
        dtsCategoryService.delete(id);
        return ResponseUtil.ok();
    }
}
